﻿/*{
author=pk2.2（风铃等）
version=2.0
date=2023/03/19
}*/

/***
允许AI和玩家一样,可以花钱来安置流民增加人口.
***/

// ## 2023/05/03 # 铃 # 修复bug,美化显示。 ##
// ## 2023/05/02 # 铃 # 适配新的内政系统。 ##
// ## 2023/05/01 # 铃 # 完善了新的参数。 ##
// ## 2022/12/03 # 铃 # 新做了AI的安置流民功能。 ##

namespace AI安置流民
{

	// ================ CUSTOMIZE ================

	const int AI安置流民_发动时期 = 1;
	const int AI安置流民_发动概率_城市 = 0;


	const int pop_gain = 10000;
	const int gold_num = 3000;

	// ===========================================

	class Main
	{

		Main()
		{
			pk::bind(202, pk::trigger202_t(onAIRunningOrder));
		}

		void onAIRunningOrder(pk::ai_context @context, pk::building @building, int cmd)
		{
			if (cmd == 据点AI_安置流民)
			{
				int force_id;
				int pop_gain_final;
				pk::force@ force = context.force;
				int buildingid = building.get_id();

				int province_id = pk::get_province_id(building.pos);
				float deserter = ch::get_set_p(0).province_deserter[province_id];
				if (deserter < 3000)
					return;

				auto base_t = ch::get_base_p(buildingid);

				force_id = building.get_force_id();
				
				if (buildingid < 42)
					pop_gain_final = pop_gain;
				else
					pop_gain_final = int(pop_gain * 0.5);

				pop_gain_final = pk::min(int(deserter), pop_gain_final + pk::rand(1000)); // 增加随机数

				// 钱越多触发概率越高,小城減半.兵役人口不足時,大幅增加概率
				// 安置的同時增加30%兵役人口.
				if (pk::rand_bool(AI安置流民_发动概率_城市 + int(log(pk::get_gold(building)) * 0.3) + (base_t.mil_pop_all < 30000 ? 3 : 0)) and pk::get_gold(building) > 15000 and !pk::enemies_around(building))
				{

					pk::add_gold(building, -gold_num, true);
					ch::add_population(building.get_id(), int(pop_gain_final * 0.7));
					ch::add_mil_pop_all(building.get_id(), int(pop_gain_final * 0.3));
					ch::get_set_p(0).province_deserter[province_id] -= pop_gain_final;

					pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("\x1b[1x{}安置了州流民：\x1b[1x{}人", pk::decode(pk::get_name(building)), int(pop_gain_final), 0)));
				}

			} 
		}

	} // class

	Main main;
}